package com.ssy.controller;

import com.ssy.entity.Clazz;
import com.ssy.entity.ClazzQueryParam;
import com.ssy.entity.PageBean;
import com.ssy.entity.Result;
import com.ssy.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    /**
     * 分页查询班级信息(及根据条件进行查询)
     *
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result list(ClazzQueryParam clazzQueryParam) {
        log.info("查询所有班级");
        PageBean pageBean = clazzService.list(clazzQueryParam);
        return Result.success(pageBean);
    }

    /**
     * 添加班级信息
     */
    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        log.info("添加班级: {}", clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    /**
     * 删除班级信息(根据id删除)
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除班级: {}", id);
        clazzService.delete(id);
        return Result.success();
    }

    /**
     * 根据id查询班级信息(查询回显)
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("查询班级信息: {}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 修改班级信息
     */
    @PutMapping
    public Result update(@RequestBody Clazz clazz) {
        log.info("修改班级信息: {}", clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    /**
     * 查询班级所有信息
     */
    @GetMapping("/list")
    public Result listAll() {
        log.info("查询所有班级信息");
        List<Clazz> clazz = clazzService.listAll();
        return Result.success(clazz);
    }
}
